package com.base.controller;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.voip.SelfShopCategory;
import com.base.po.voip.Users;
import com.base.service.LogsService;
import com.base.service.SelfShopCategoryService;
import com.base.service.UsersService;
import com.base.utils.IP;
import com.base.utils.Produce;

@Controller
@RequestMapping("/admin")
public class add_SelfShopCategory {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private SelfShopCategoryService selfShopCategoryService;
	@Autowired
	private UsersService usersService;

	// 添加一级分类
	@RequestMapping("/add_SelfShopCategory")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		if (adminUserId == null) {
			modelAndView.setViewName("jsp/login");
			return modelAndView;
		}
        
		modelAndView.setViewName("jsp/add_SelfShopCategory");
		return modelAndView;
	}
	
	//添加二级分类
	@RequestMapping("/add_SecondCategory")
	public ModelAndView add_SecondCategory(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		if (adminUserId == null) {
			modelAndView.setViewName("jsp/login");
			return modelAndView;
		}
        
		Users users = usersService.selectByPrimaryKey(adminUserId);
		List<SelfShopCategory> selfShopCategory = null;
		
		if(users.getRole() == 0 || users.getRole() == 1) {
			selfShopCategory = selfShopCategoryService.selectByLevel1(adminUserId,adminUserId);
		}
		if(users.getRole() == 2) {
			selfShopCategory = selfShopCategoryService.selectByLevel1(users.getParentid(),adminUserId);
		}
		
		request.setAttribute("selfShopCategory", selfShopCategory);
		modelAndView.setViewName("jsp/add_SelfShopCategory");
		return modelAndView;
	}
	
	
	String SUCCESS = "success";

	// 添加广告
	@RequestMapping(value = "/insertSelfShopCategory", method = RequestMethod.POST)
	public void insertSelfShopCategory(HttpServletRequest request, HttpServletResponse response, 
			String agentid, String category, Integer level, String pic, String firstid,
			String topid, Byte datastatus, Integer grade) throws IOException {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String adminId = (String) request.getSession().getAttribute("adminUserId");
		if (adminId == null) {
			out.print("请刷新页面重试！");
			return;
		}	
		Users users = usersService.selectByPrimaryKey(adminId);
		List<SelfShopCategory> selfShopCategory = null;
		
		if(users.getRole() == 0 || users.getRole() == 1) {
			selfShopCategory = selfShopCategoryService.selectByParentIDAndShopID(adminId,adminId);
		}
		if(users.getRole() == 2) {
			selfShopCategory = selfShopCategoryService.selectByParentIDAndShopID(users.getParentid(),adminId);
		}
		
		if(selfShopCategory.size() == 0) {
			grade = 0;
		}else {
			for(int i=0;i<selfShopCategory.size();i++) {
				grade = (selfShopCategory.get(0).getGrade())+1;
			}
		}
		
		String id = Produce.getUUID(); 
		
		
		int resultCode = 0;
		//暂定一级分类
		level = 1;
		//设置二级分类
		if(firstid != null && !firstid.equals("undefined")) {
			level = 2;
			topid = firstid;
		}
		
		if(users.getRole() == 1) {
			resultCode = selfShopCategoryService.insert(new SelfShopCategory(id, adminId, adminId, category,
					level, topid, pic, grade, datastatus));
		}
		if(users.getRole() == 2) {
			resultCode = selfShopCategoryService.insert(new SelfShopCategory(id, users.getParentid(), adminId, 
					category, level, topid, pic, grade, datastatus));
		}
		
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		if (resultCode == 1) {
			logsService.insert(CommContent.TYPE_UPDATE, "添加"+category+"自营商城分类信息", usernamesys,
					true, IP.getIpAddr(request));
			out.print(SUCCESS);
		} else {
			logsService.insert(CommContent.TYPE_UPDATE, "添加"+category+"自营商城分类失败", usernamesys,
					true, IP.getIpAddr(request));
			out.print("更改失败，请您重试");
		}
	}

}
